package first.codeTop.byteDance;

import java.util.HashMap;

/**
 * @ClassName Leetcode1
 * @description:
 * @author: 荣燊
 * @create: 2021-07-25 14:28
 **/
public class Leetcode1 {
    // 两数之和,hash，因为返回的是数组的下标
    public static void main(String[] args) {
        int[] nums = {2,7,11,15,3};
        System.out.println(twoSum(nums, 10));
    }

    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i=0; i<nums.length; i++) {
            if (map.containsKey(target-nums[i])) {
                return new int[]{map.get(target-nums[i]), i};
            }
            map.put(nums[i], i);
        }
        return new int[0];
    }
}
